package cn.edu.hqu.cst.android.ljc.client;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;


public class MainActivity extends AppCompatActivity {
    EditText et_main_content;
    TextView tv_receive_content;
    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_main_content = findViewById(R.id.et_main_content);
        tv_receive_content = findViewById(R.id.tv_receive_content);

        handler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what == 0x123){
                    tv_receive_content.setText(tv_receive_content.getText().toString() + msg.getData().getString("receive"));
                }
            }
        };
        new Thread(new ReceiveThread(handler)).start();
    }

    public void send(View view) {
        Message msg = new Message();
        msg.what = 0x1234;
        Bundle bundle = new Bundle();
        bundle.putString("sendContent",et_main_content.getText().toString());
        msg.setData(bundle);
        ClientThread.sendHandler.sendMessage(msg);
    }

    public void connect(View view) {
        new Thread(new ClientThread()).start();
    }
}

class ReceiveThread implements Runnable{
    DatagramSocket localDs = null;
    private final static int localPort = 5001;
    byte[] buf = new byte[1024];
    Handler handler;

    public ReceiveThread(Handler handler) {
        this.handler = handler;
        try {
            localDs = new DatagramSocket(localPort);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (true){
            DatagramPacket localDp = new DatagramPacket(buf, 1024);
            try {
                localDs.receive(localDp);
                String rec = "服务端发来消息：";
                rec += new String(localDp.getData(),localDp.getOffset(),localDp.getLength());
                rec += '\n';
                Message msg = new Message();
                msg.what=0x123;
                Bundle bundle = new Bundle();
                bundle.putString("receive",rec);
                msg.setData(bundle);
                handler.sendMessage(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}

class ClientThread implements Runnable {
    DatagramSocket remoteDs = null;
    InetAddress remoteAddr = null;
    private final static int remotePort = 5000;
    public static Handler sendHandler;

    public ClientThread() {
        try {
            remoteDs = new DatagramSocket();
            remoteAddr = InetAddress.getByName("127.0.0.1");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                sendHandler = new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        if (msg.what == 0x1234) {
                            byte[] buffer = msg.getData().getString("sendContent").getBytes();
                            DatagramPacket remoteDp = new DatagramPacket(buffer, buffer.length, remoteAddr, remotePort);
                            try {
                                remoteDs.send(remoteDp);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                };
            Looper.loop();
            }
        }.start();
    }
}



